package com.lcz.mobilesafe.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.lcz.mobilesafe.R;
import com.lcz.mobilesafe.adapter.HomeAdapter;
import com.lcz.mobilesafe.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主页面
 */
public class HomeActivity extends AppCompatActivity {

    GridView gvHome;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pwd2)
    EditText etPwd2;
    @BindView(R.id.bt_ok)
    Button btOk;
    @BindView(R.id.bt_cancle)
    Button btCancle;
    private SharedPreferences spf;
    private AlertDialog setPwdDialog;
    private AlertDialog inputPwdDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        spf = getSharedPreferences("config", MODE_PRIVATE);
        gvHome = (GridView) findViewById(R.id.gv_home);
        gvHome.setAdapter(new HomeAdapter());
        gvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        //弹出密码输出框
                        showPasswordDialog();
                        break;
                    case 8:
                        startActivity(new Intent(HomeActivity.this, SettingActivity.class));
                        break;
                }
            }
        });
    }

    private void showPasswordDialog() {
        //判断是否设置密码
        String password = spf.getString("password", null);
        if (TextUtils.isEmpty(password)) {
            showSetPwdDialog();
        } else {
            showInputPwdDialog();
        }

    }

    private void showInputPwdDialog() {
        final View view = View.inflate(this, R.layout.input_pwd_dialog, null);
        inputPwdDialog = new AlertDialog.Builder(this).create();
        inputPwdDialog.setView(view, 0, 0, 0, 0);
        inputPwdDialog.show();
        final EditText et_input_pwd = (EditText) view.findViewById(R.id.et_input_pwd);
        view.findViewById(R.id.bt_input_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputPwd = et_input_pwd.getText().toString();
                if (TextUtils.isEmpty(inputPwd)) {
                    ToastUtils.show("输入框内容不能为空！！");
                } else {
                    String savePwd = spf.getString("password", null);
                    if (inputPwd.equals(savePwd)) {
                        ToastUtils.show("登录成功！！");
                        inputPwdDialog.dismiss();
                    } else {
                        ToastUtils.show("密码错误！！");
                    }
                }
            }
        });
        view.findViewById(R.id.bt_input_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputPwdDialog.dismiss();
            }
        });

    }

    private void showSetPwdDialog() {
        View view = View.inflate(this, R.layout.set_pwd_dialog, null);
        ButterKnife.bind(this, view);
        setPwdDialog = new AlertDialog.Builder(this).create();
        setPwdDialog.setView(view, 0, 0, 0, 0);
        setPwdDialog.show();
    }

    @OnClick({R.id.bt_ok, R.id.bt_cancle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_ok:
                String pwd = etPwd.getText().toString();
                String pwd2 = etPwd2.getText().toString();
                if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd2)) {
                    ToastUtils.show("输入框内容不能为空！！");
                } else {
                    if (pwd.equals(pwd2)) {
                        //保存密码至spf
                        spf.edit().putString("password", pwd).commit();
                        setPwdDialog.dismiss();

                    } else {
                        ToastUtils.show("两次密码不一致！！");
                    }
                }
                break;
            case R.id.bt_cancle:
                setPwdDialog.dismiss();
                break;
        }
    }

}
